<template>
	<view>
		<nav-bar   fontColor="#FFFFFF" title="盘点">
			<!-- <image v-if="Qrcode" @click="startCheck" src="./rfid.png" style="width: 60rpx;height: 60rpx;margin-right: 10rpx;" slot="right"></image> -->
		</nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>

		<!-- <view v-if="Qrcode"> -->
		<view v-if="!Qrcode">
			<view @click="saoma()" class="imgCode">
				<image class="qrImg" src="../../static/icon/scan.png"></image>
				<view class="qrTitle">请扫描库位二维码标签</view>
			</view>
		</view>
		<view v-else>
			<view class="orderNumber">库位号：{{Qrcode}}</view>
			<view class="orderNumber">数{{' '}}量：{{dataList.length}}个</view>
			<view class="orderNumber">盘点数：{{textData.length}}个</view>
			<view v-for="(item,index) in dataList">
				<uni-list>
					<uni-list-item :index="index+1" :disabled="item.checkNo == 0 ? false : true"
						:title="'物品名称:'+item.materialName + '\n物品编号:'+item.materialNo"
						:note="'案件名称:'+item.caseName + '\n'+ '案件编号:'+item.caseNo" link="reLaunch" clickable>
					</uni-list-item>
				</uni-list>

				<!-- 	<uni-list>
					<uni-list-item thumb="/static/icon/event_icon/workOrder.png" @click="goCheckList(item.rfidCode)"
						:title="'物品名称:'+item.itemName + '\n物品编号:'+item.serialNo"
						:note="'案件名称:'+item.caseName + '\n'+ '案件编号:'+item.caseNo + '\n' + '库位编号:' + item.storeLotNo" link="reLaunch" clickable>
					</uni-list-item>
				</uni-list> -->
			</view>
			<view style="height: 120rpx;">
				<view style="position: fixed;bottom: 20rpx;width: 100%;z-index: 999;">
					<!-- <button @click="startCheck" type="primary"
						style="width: 90%;margin: 0 auto;background:#3B5DE0;margin-bottom: 20rpx;">清除盘点</button>
					<button @click="goNext" type="primary"
						style="width: 90%;margin: 0 auto;background:#3B5DE0;">提交盘点</button> -->
						<view class="buttonBox">
							<button class="button_fix" @click="startCheck" size="default">重置</button>
							<button class="button_fix" @click="goNext" size="default">提交</button>
						</view>
				</view>
			</view>
		</view>
		<!-- </view> -->
		<!-- <view>
			<view @click="saoma" class="imgCode top">
				<image class="qrImg" src="../../static/icon/scan.png"></image>
				<view class="qrTitle">请扫描二维码标签</view>
			</view>
		</view> -->
		<!-- </mescroll-body> -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="3000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog :deleteIcon="true" :dataList="textData" title="确认信息" ref="inputClose" @confirm="confirm"
				mode="scroll">
			</uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="hintDialog" type="dialog">
			<uni-popup-dialog type="info" :beforeClose="true" cancelText="返回首页" confirmText="继续盘点" title="提示"
				content="还有其他物品需要继续盘点吗？" @confirm="hintConfirm" @close="hintClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	var main = null;
	var receiver = null;
	import uniList from '@/components/uni-list/uni-list.vue';
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			uniList,
		},
		data() {
			return {
				// btnnum: 0, //topbar
				Qrcode: '', //库位号
				orderNumber: "BC010302",
				//搜索内容
				downOption: {
					native: false,
					use: true, // 是否启用下拉刷新; 默认true
					auto: true
				},
				upOption: {
					use: true, // 是否启用上拉加载; 默认true
					auto: false,
					noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
				},
				dataList: [],
				userInfo: {},
				repeatRfid: [],
				rfId: [],
				msgType: 'success',
				messageText: '这是一条成功提示',
				textData: []
			};
		},
		onLoad() {
			// this.startCheck();
			// this.getDataList();
			// this.test();
			this.get_gb();

		},
		onBackPress() {
			if (receiver != null) {
				main.unregisterReceiver(receiver)
			}
		},
		onShow() {

		},
		//方法
		methods: {
			hintConfirm() {
				this.$refs.hintDialog.close();
			},
			hintClose() {
				this.$refs.hintDialog.close();
				uni.navigateBack({
					delta: 1
				});
			},
			messageToggle(type, msg) {
				this.msgType = type
				this.messageText = msg
				this.$refs.message.open();
			},
			goNext() {
				if (this.textData == '') {
					this.messageToggle('error', '请扫描RFID射频标签');
				} else {
					this.$refs.inputDialog.open();
				}
			},
			confirm() {
				var that = this;
				that.$refs.inputDialog.close();
				if (that.textData == '') {
					that.messageToggle('error', '请输入完整的数据！')
				} else {
					that.submit();
				}
			},
			test() {
				let opt = {
					url: 'web/tWhMaterialCheckLine/processCheck',
					method: 'POST'
				};
				let params = {
					// rfidCode: ['E280110C2000794B15790A8B'],
					rfidCodes: ['4234234'],
					counts: ['1']
				}
				this.$http.httpTokenRequest(opt, params).then(res => {
					console.log(res)
				})
			},
			get_gb() {
				var that = this;
				main = plus.android.runtimeMainActivity();
				receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						var data = intent.getExtra("data");
						if (data) {
							if (that.textData == '') {
								that.rfId = [];
								that.repeatRfid = [];
							}
							console.log(data)
							that.repeatRfid.push(data)
							for (let i = 0; i < that.repeatRfid.length; i++) {
								if (that.rfId.indexOf(that.repeatRfid[i]) == -1) {
									that.rfId.push(that.repeatRfid[i])
									that.pushArray(that.repeatRfid[i]);
									console.log(that.rfId)
								}
							}
						}
					}
				});
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				let filter = new IntentFilter();
				filter.addAction('android.rfid.epccode');
				var info = main.registerReceiver(receiver, filter)
				// main.unregisterReceiver(receiver)
			},
			pushArray(e) {
				var that = this;
				let opt = {
					url: 'oms/tbRelateItemTakeDetail/search',
					method: 'POST'
				};
				let params = {
					rfidCode: e
				};
				that.$http.httpTokenRequest(opt, params).then(res => {
					that.textData.push({
						itemName: res.data.data[0].itemName,
						caseName: res.data.data[0].caseName,
						serialNo: res.data.data[0].serialNo,
						caseNo: res.data.data[0].caseNo,
						rfidCode: e
					})
					console.log(that.textData)
				})
			},
			startCheck() {
				let opt = {
					url: 'web/tWhMaterialCheckLine/startCheck',
					method: 'POST'
				};
				let params = {
					locationNo: this.Qrcode
				};
				this.$http.httpTokenRequest(opt, params).then(res => {
					if (res.data.code == 200) {
						console.log('开始盘点接口调用成功')
						this.messageToggle('success', res.data.msg)
						this.getDataList()
					} else {
						this.messageToggle('error', res.data.msg)
					}
				})
			},
			getDataList() {
				let opt = {
					url: 'web/tbWmsMaterialInLocation/search',
					method: 'POST'
				};
				let param = {
					locationNo: this.Qrcode
				};
				this.$http.httpTokenRequest(opt, param).then(req => {
					if (req.data.code == 200) {
						this.dataList = req.data.data

					} else {
						this.messageToggle('error', req.data.msg)
					}

				})
			},
			submit() {
				var that = this;
				let countList = [];
				let codeRfid = [];
				for (let i = 0; i < that.textData.length; i++) {
					codeRfid.push(that.textData[i].rfidCode)
					countList.push('1')
				}
				console.log(codeRfid)
				console.log(countList)
				let opt = {
					url: 'web/tWhMaterialCheckLine/processCheck',
					method: 'POST'
				};
				let params = {
					rfidCodes: codeRfid,
					counts: countList
				}
				this.$http.httpTokenRequest(opt, params).then(res => {
					console.log(res)
					if (res.data.code == 200) {
						this.messageToggle('success', res.data.msg)
						this.$refs.hintDialog.open();
						this.rfId = []
						this.repeatRfid = []
						this.textData = []
						this.getDataList();
						// this.getDataList();
					} else {
						this.messageToggle('error', res.data.msg)
						this.rfId = []
						this.repeatRfid = []
						this.textData = []
						this.getDataList();
					}
				})
			},
			saoma() {
				var that = this;
				uni.scanCode({
					success: function(res) {
						let code1 = res.result.slice(0,2)
						let code2 = res.result.slice(2,4)
						let code3 = res.result.slice(4,6)
						let code4 = res.result.slice(6,8)
						that.Qrcode = code1 + '-' + code2 + '-' + code3 + '-' + code4
						that.getDataList();
					}
				});
			},
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},

			/*下拉刷新的回调 */
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				this.loadData(page.num);
			},
			loadData(pageNo) {
				let opts = {
					url: 'mesg/yunheFireOrEventMesg/search/' + pageNo + '/' + 10,
					method: 'POST'
				};
				let params = {
					eventType: '林业巡检',
					// contactId: uni.getStorageSync('userInfo').data.sysUser.id,
					// status: 'COMPLETED'
				};
				this.$http.httpTokenRequest(opts, params).then(res => {
						if (res.data.code == 200) {
							this.mescroll.endSuccess(res.data.data.records.length)
							if (pageNo == 1) {
								this.dataList = res.data.data.records;
							} else {
								this.dataList = this.dataList.concat(res.data.data.records);
							}
							this.orderNumber = res.data.data.total
							console.log(this.dataList)
							uni.hideLoading();
						} else if (res.data.code == 401) {
							uni.hideLoading();
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								position: 'center'
							});
							setTimeout(() => {
								uni.navigateTo({
									url: './homePage'
								})
							}, 1500)
						} else {
							uni.hideLoading();
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								position: 'center'
							});
						}
					})
					.catch(res => {
						this.mescroll.endErr();
						uni.hideLoading();
					});
			}
		},
		//页面下来刷新
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 2000);
		},
		//页面上拉触底
		onReachBottom() {
			console.log('上拉');
		},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	page {
		background: #FFFFFF;
	}

	.orderNumber {
		margin: 10rpx 30rpx 10rpx 30rpx;
		font-size: 35rpx;
		font-weight: 700;
		letter-spacing: 3rpx;
		text-align: center;
	}

	.imgCode {
		position: relative;
		background: linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) left bottom,
			linear-gradient(#000000, #000000) left bottom;
		background-repeat: no-repeat;
		background-size: 2px 20px, 20px 2px;
		height: 400rpx;
		width: 400rpx;
		margin: 0 auto;
		margin-top: 45%;
	}

	// .top {
	// 	margin-top: 35%;
	// }

	.qrImg {
		width: 250rpx;
		height: 250rpx;
		left: 20%;
		top: 15%;
	}

	.qrTitle {
		font-size: 28rpx;
		color: #3B5DE0;
		margin-top: 90rpx;
		text-align: center;
		letter-spacing: 3rpx;
	}
	.buttonBox {
		width: 80%;
		margin: 0 auto;
		display: flex;
		margin-top: 200rpx;
		// z-index: 999;
	}
	
	.button_fix {
		width: 40%;
		height: 70rpx;
		margin: 0 auto;
		color: #FFFFFF;
		background: #2749C8;
		font-weight: bold;
		letter-spacing: 5rpx;
		// opacity: 0.9;
		font-size: 28rpx;
		line-height: 70rpx;
		
	}
</style>
